package com.beta.nfc_beta;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import com.beta.util.BasicTagTechnologyWrapper;
import com.beta.util.LogHelper;
import com.beta.util.TagHelper;
import com.beta.util.TextHelper;

import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.os.Bundle;
import android.app.Activity;
import android.text.SpannableString;
import android.util.Log;
import android.view.Menu;

public class PayActivity extends Activity {
	private boolean mDebugLogging = false;
	private boolean mMask = false;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pay);
	}

	
	/*private void doReplayPCD(Tag tag, Bundle pcdRequests, Bundle tagTransactions) {
		Bundle responses = new Bundle();
		BasicTagTechnologyWrapper tagTech = null;
		try {
			//TODO:add support for more tag types
			Class[] supportedTags = new Class[] { IsoDep.class };			
			String[] tech = tag.getTechList();	    	
	    	for (String s: tech) {
	
	    		for(Class c: supportedTags) {
	    			if (s.equals(c.getName())) {
	    				try {
							tagTech = new BasicTagTechnologyWrapper(tag, c.getName());
							break;
						} catch (IllegalArgumentException e) {
							log(e);
						} catch (ClassNotFoundException e) {
							log(e);
						} catch (NoSuchMethodException e) {
							log(e);
						} catch (IllegalAccessException e) {
							log(e);
						} catch (InvocationTargetException e) {
							log(e);
						}    				
	    			}
	    		}    		
			}
	    	if (tagTech != null) {
	    		tagTech.connect();
	    		boolean connected = tagTech.isConnected(); 
	    		log("isConnected: " + connected);
	    		if (!connected) return;
	    		
	    		//first store ID
	    		responses.putByteArray(String.valueOf(0), tag.getId());
	    		//SpannableString msg = new SpannableString(TextHelper.byteArrayToHexString(tag.getId()));
		    	//msg.setSpan(new UnderlineSpan(), 0, 4, 0);				    				    	
				updateData(msg);	    	
				boolean foundCC = false;
	    		for(int i=0; i < pcdRequests.size(); i++) {
	    			if (foundCC) {
	    				updateData(""); //print newline. this will probably cause formatting problems later
	    			}
	    			byte[] tmp = pcdRequests.getByteArray(String.valueOf(i));
					msg = new SpannableString(pcdStr + TextHelper.byteArrayToHexString(tmp));
			    	msg.setSpan(new UnderlineSpan(), 0, 4, 0);				    				    	
					updateData(msg);
	    			byte[] reply = tagTech.transceive(tmp);

	    			responses.putByteArray(String.valueOf(i+1), reply);
	    			if (mMask && reply != null && reply[0] == 0x70) {
	    				msg = new SpannableString(tagStr + getString(R.string.masked));
	    			}
	    			else {
	    				msg = new SpannableString(tagStr + TextHelper.byteArrayToHexString(reply));
	    			}
			    	msg.setSpan(new UnderlineSpan(), 0, 4, 0);				    				    	
					updateData(msg);	    				    		
	    			
					if (tagTransactions != null) {
						if (i + 1 < tagTransactions.size() ) {
							if (Arrays.equals(reply, tagTransactions.getByteArray(String.valueOf(i + 1)))) {
								log(getString(R.string.equal));
								//updateStatus(getString(R.string.equal));
							}
							else {
								log(getString(R.string.not_equal));
log("org: " + TextHelper.byteArrayToHexString(tagTransactions.getByteArray(String.valueOf(i + 1))));
log("new : " + TextHelper.byteArrayToHexString(reply));
								//updateStatus(getString(R.string.not_equal));
	//updateStatus("org: " + TextHelper.byteArrayToHexString(tagTransactions.getByteArray(String.valueOf(i + 1))));
	//updateStatus("new : " + TextHelper.byteArrayToHexString(reply));					
							}
						}
						else {
							log("index to responses out of bounds");
							updateStatus(getString(R.string.index_out_bounds));
						}
					}

	    			if (reply != null && reply[0] == 0x70) {
	    				updateData("\n" + TagHelper.parseCC(reply, pcdRequests.getByteArray(String.valueOf(i - 1)), mMask));
	    				foundCC = true;
	    				if (i == pcdRequests.size() - 1) {
		    				log(getString(R.string.finished_reading));
		    				updateStatus(getString(R.string.finished_reading));
	    				}
	    			}
					else if (reply != null && reply.length > 3 && reply[0] == 0x77 && reply[2] == (byte)0x9f) {
						updateData("\n" + TagHelper.parseCryptogram(reply, tmp)); //previous pcdRequest
	    				log(getString(R.string.finished_reading));
	    				updateStatus(getString(R.string.finished_reading));
					}

	    		}
	    		
	    	}
	    	else {
	    		log(getString(R.string.unsupported_tag));
	    		updateStatus(getString(R.string.unsupported_tag));
	    	}
		} catch (IllegalStateException e) {
			log(e);
			updateStatus(e.toString());
		} catch (IOException e) {
			log(e);
			updateStatus(e.toString());
		}
		finally {			
			storeTransactionsAndBreak(pcdRequests, responses);
			if (tagTech != null) {
				try {
					tagTech.close();
				} catch (IOException e) {
					log(e);
				}
			}
		}

		//log(getString(R.string.lost_connection));
		//updateStatus(getString(R.string.lost_connection));
	}
*/

	private void updateStatus(String string) {
		// TODO Auto-generated method stub
		
	}


	private void updateData(String string) {
		// TODO Auto-generated method stub
		
	}
	/*private void storeTransactionsAndBreak(Bundle requests, Bundle responses) {
		final Bundle session = new Bundle();
		session.putBundle("requests", requests);
		session.putBundle("responses", responses);
        mDataView.setOnLongClickListener(getTransactionsTextViewLongClickListener());
		mDataTable.post(new Runnable() {
			@Override
			public void run() {
				if (mDataView!= null && mDataView.getText().length() > 0) {
					//TODO: ...might be race condition here XXX
					mSessions.putBundle(String.valueOf(mSessions.size()), session);										
					addLineBreak(mSessions.size() - 1);
					mDataTab.fullScroll(ScrollView.FOCUS_DOWN);
					mDataView = null;
				}					
			}
		});			
	}*/
	 private void log(Object msg) {    	
	    	if (mDebugLogging) {
	    		LogHelper.log(this, msg);
	    	  }
	       }	
	    



}
